package com.jcfk.eam.controller.main;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;

import com.jcfk.eam.domain.query.base.EamRepairPlanQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamRepairPlanDetailQuery;
import com.jcfk.eam.domain.dto.base.EamRepairPlanDetailDTO;
import com.jcfk.eam.domain.dto.base.EamRepairPlanDetailItemDTO;
import com.jcfk.eam.service.main.EamRepairPlanDetailService;

import java.util.List;

/**
 * 计划维修明细
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-21
 */
@Api(tags = {"计划维修明细管理"})
@RestController
@RequestMapping("eamrepairplandetail")
public class EamRepairPlanDetailController {
    @Autowired
    private EamRepairPlanDetailService eamRepairPlanDetailService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamRepairPlanDetailQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询计划维修明细")
    @PostMapping("/queryByPage")
    public RetPage<EamRepairPlanDetailDTO> queryByPage(@RequestBody ReqPageParam<EamRepairPlanDetailQuery> requestParam) {
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamRepairPlanDetailQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamRepairPlanDetailDTO> page = new DataPage<EamRepairPlanDetailDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamRepairPlanDetailDTO.class);
        page = eamRepairPlanDetailService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamRepairPlanDetailQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取计划维修明细列表", notes = "获取计划维修明细列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamRepairPlanDetailItemDTO>> getDetails(@RequestBody EamRepairPlanDetailQuery query) {

        List<EamRepairPlanDetailItemDTO> list = eamRepairPlanDetailService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamRepairPlanDetailQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取计划维修明细列表", notes = "获取计划维修明细列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamRepairPlanDetailDTO>> getList(@RequestBody EamRepairPlanDetailQuery query) {

        List<EamRepairPlanDetailDTO> list = eamRepairPlanDetailService.getList(query);

        return RetResult.ok(list);
    }


    /**
     *  计划维修明细
     * 根据id 查询详情
     */
    @ApiOperation("查询计划维修明细")
    @GetMapping("/get/{id}")
    public RetResult<EamRepairPlanDetailDTO> get(@PathVariable("id") String id) {
            EamRepairPlanDetailDTO item = eamRepairPlanDetailService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理计划维修明细
     */
    @ApiOperation("保存计划维修明细")
    @PostMapping("/save")
    @RequestLogHeader(modular = "计划维修明细管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamRepairPlanDetailDTO eamRepairPlanDetail) {
        // 数据校验
        RetResult ret = eamRepairPlanDetailService.isValid(eamRepairPlanDetail);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamRepairPlanDetail.getId())){
            // 新增
                eamRepairPlanDetailService.insert(eamRepairPlanDetail);
        } else{
            // 更新
                eamRepairPlanDetailService.update(eamRepairPlanDetail);
        }
        return RetResult.ok();

    }


    /**
     *  批量删除计划维修明细
     */
    @ApiOperation("批量删除计划维修明细")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "计划维修明细管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamRepairPlanDetailDTO eamRepairPlanDetail) {
        return RetResult.ok(eamRepairPlanDetailService.logicDeleteList(eamRepairPlanDetail));
    }


    /**
     *  删除计划维修明细
     */
    @ApiOperation("删除计划维修明细")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "计划维修明细管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamRepairPlanDetailService.deleteItem(id);
        return RetResult.ok();
    }
}
